{include file="public/header" /}

<body class="gray-bg">
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>编辑广告</h5>
                </div>
                <div class="ibox-content">
                    <form action="" class="layui-form layui-form-pane">
                        <div class="layui-form-item">
                            <label class="layui-form-label">广告名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="title" placeholder="请输入广告名称" autocomplete="off"
                                       class="layui-input" lay-verify="required" lay-verType="tips" value="{$ad['title']}">
                            </div>
                            <div class="layui-form-mid layui-word-aux">只做为系统区别、前端页面不会展示</div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">广告位</label>
                            <div class="layui-input-block">
                                <select name="ad_position_id" lay-verify="required" lay-verType="tips">
                                    <option value="">==== 请选择 ====</option>
                                    {foreach name="position" id="value"}
                                    <option value="{$value['id']}" {if condition="$value['id'] eq $ad['ad_position_id']"}selected{/if}>{$value['name']}</option>
                                    {/foreach}
                                </select>
                            </div>
                            <div class="layui-form-mid layui-word-aux">广告所属类别</div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">广告图</label>
                            <div class="layui-input-block">
                                <div class="layui-upload">
                                    <input type="hidden" name="images" value="{$ad['images']}" id="images">
                                    <button type="button" class="layui-btn" id="test1">选择图片</button>
                                    <div class="layui-upload-list" style="height: 120px;">
                                        <img class="layui-upload-img" id="demo1" style="max-height: 100%;" src="/static/uploads/images/{$ad['images']}">
                                        <p id="demoText"></p>
                                    </div>
                                </div>
                                <div class="layui-form-mid layui-word-aux">请上传广告图片</div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">链接</label>
                            <div class="layui-input-block">
                                <input type="url" class="layui-input" name="link_url" value="{$ad['link_url']}">
                                <div class="layui-form-mid layui-word-aux">点击广告图片跳转地址、例：http://www.baidu.com</div>
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">投放时间</label>
                            <div class="layui-input-block">
                                <input type="text" name="time" placeholder="请选择时间" id="time" class="layui-input"
                                       lay-verify="required" lay-verType="tips" value="{$ad['time']}">
                            </div>
                            <div class="layui-form-mid layui-word-aux">广告将在这个时间范围内播放</div>
                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label">状态</label>
                            <div class="layui-input-block">
                                <input type="radio" name="status" value="1" title="开启" {if condition="$ad['status'] eq 1"}checked{/if}/>
                                <input type="radio" name="status" value="0" title="关闭" {if condition="$ad['status'] eq 0"}checked{/if}/>
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label">排序</label>
                            <div class="layui-input-block">
                                <input type="number" class="layui-input" name="orderby" lay-verify="required|number"
                                       lay-verType="tips" value="{$ad['orderby']}">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <input type="hidden" name="id" value="{$ad['id']}">
                                <button class="btn btn-primary" type="submit" lay-submit lay-filter="edit_ad"><i
                                        class="fa fa-save"></i> 保存
                                </button>&nbsp;&nbsp;&nbsp;
                                <a class="btn btn-danger" href="javascript:history.go(-1);"><i class="fa fa-close"></i>
                                    返回</a>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    var laydate = layui.laydate, form = layui.form, upload = layui.upload;
    //日期时间范围选择
    laydate.render({
        elem: '#time',
        type: 'datetime',
        calendar:true,
        // value:"{$ad['time']}",
        range: '~' //或 range: '~' 来自定义分割字符
    });
    /**
     * 编辑广告
     */
    form.on('submit(edit_ad)', function (data) {
        var data = data.field;
        if (!data.images) {
            layer.msg('请上传广告图');
            return false;
        }
        $.post("{:url('edit_ad')}", data, function (result) {
            if (result.code === 1) {
                layer.msg(result.msg, {time: 1500,icon:1}, function () {
                    location.href = "{:url('ad/index')}"
                })
            } else {
                layer.msg(result.msg,{icon:2});
            }
        });
        return false;
    });
    /**
     *
     */
    var uploadInst = upload.render({
        elem: '#test1',
        url: "{:url('Upload/uploads')}",
        data:{path:'ad'},
        before: function (obj) {
            //预读本地文件示例，不支持ie8
            obj.preview(function (index, file, result) {
                $('#demo1').attr('src', result); //图片链接（base64）
            });
        },
        done: function (res) {
            //如果上传失败
            if (res.code > 0) {
                return layer.msg('上传失败');
            }
            //上传成功
            $('#images').val(res.data);
        },
        error: function () {
            //演示失败状态，并实现重传
            var demoText = $('#demoText');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
            demoText.find('.demo-reload').on('click', function () {
                uploadInst.upload();
            });
        }
    });
</script>
</body>
</html>
